
let createNewPromise = (url,data,method,promiseResolve)=>{
	return new Promise(() => {
		wx.request({
			url:url,
			data:data,
			method:method,
			header:{
				token: getApp().globalData.token
			},
			success:(res)=>{ 
				promiseResolve(res.data); 
			},
			fail:(err)=>{    
			  	console.log(err);  
			},
		})
	})
}

let getResult = (url,params,method)=>{
	let promiseTemp = null;
	let promiseResolve = null;

	async function fun(){
	    promiseTemp = new Promise(resolve => promiseResolve = resolve); 
		createNewPromise(url,params,method,promiseResolve);
		let res = await promiseTemp;
		return res;
	}
	return fun()
}

//课程笔记
const getCourseNote = (courseId)=>{
	let url = getApp().globalData.baseUrl + "course/note";
	let data = {
		courseId:courseId
	}
	return getResult(url,data,"GET");
}

//章节笔记
const getSectionNote = (sectionId,kind,current)=>{
	let url = getApp().globalData.baseUrl + "course/secNote";
	let data = {
		sectionId:sectionId,
		kind:kind,
		currenrt:current,
		size:getApp().globalData.size
	}
	return getResult(url,data,"GET");
}

//添加笔记
const addNote = (courseId,title,content)=>{
	let url = getApp().globalData.baseUrl + "course/note/add";
	let data = {
		sectionId:courseId,
		title:title,
		content:content
	}
	return getResult(url,data,"POST");
}

//删除笔记
const delNote = (sectionId)=>{
	let url = getApp().globalData.baseUrl + "course/note?nodeId="+sectionId;
	let data={};
	// wx.request({
	//   	url: url,
	//   	method: "DELETE",
	// 	header:{
	// 		token: getApp().globalData.token,
	// 		"content-type":"application/x-www-form-urlencoded",
	// 	},
	// 	success:(res)=>{
	// 		console.log(res);
	// 	},
	// 	fail:(err)=>{
	// 		console.log(err);
	// 	}
	// })
	return getResult(url,data,"DELETE");
}

//修改笔记
const editNote = (noteId,title,content)=>{
	let url = getApp().globalData.baseUrl + "course/note/update";
	let data = {
		id: noteId,
		title: title,
		content: content
	}
	return getResult(url,data,"PUT");
}

export default{
	delNote,
	editNote,
	getSectionNote,
	getCourseNote,
	addNote
}